<?php
/**
 * Question Creation/Update Popup
 * 
 * @author	Xavier VILAIN
 * @package Nawa\Survey
 * @license #license-nawa-surver#
 * 
 */
use Fox\Html\Wrapper;
use Fox\Generator;
use Fox\Html\DropList;
use Nawa\Survey\Question;
use Fox\Html\Block;
use Nawa\Survey;
use Nawa\Survey\Section;
use Nawa\Messages;
use Fox\Popup;
use Fox\Html\TextArea;
use Fox\Html\Input;
use Fox\Html\Screen;

$xview = Xview::instance();
$keys = $xview->val(array('surveyid','sectionid','questionid','parentid'));
// Load Survey
$survey = $xview->component('survey','object');

if(!empty($keys['questionid'])) {
	$mode = 'update';
	$label = Msg(Messages::BUNDLE,Messages::SURVEY_SECTION_QUESTION_TITLE);
	$question = Fox\Dao::load(Question::className(),$keys['questionid']);
} else {
	$mode = 'create';
	$label = Msg(Messages::BUNDLE,Messages::SURVEY_ADD_QUESTION);
	$question = new Question();
}
$data = $question->getData();
$screen = new Screen('mQuestion',$mode,null,true,Fox\Generator::ajaxReload('MC_sections'));
// Load Question Types
$types = Fox\Dao::finder(Question\Type::className())
			->where('active = ?',array(1))
			->find();
$screen->block()
		->add(new TextArea(array('question_content','content'),true,null,100,3))->parentObject()
		->add(new DropList(array('question_type','type'),true,false,true,3))
			->options($types,'type',Generator::call('label'));
// Additionnal Information

$block = new Block('bQuestionText',Msg(Fox\Msg::BUNDLE,Fox\Msg::TITLE_TEXT_DATA));
$block->collapsed(true)
	->add(new TextArea(array('question_helper','helper')))->parentObject();
$screen->add($block);
// Answers
$wrapper = new Wrapper('answersWrapper');
if($question->exists()) {
	$blAnswers = $xview->module(Generator::url('survey','questions','answers',
								array('questionid' => $keys['questionid'],'ajax' => false)));
	if(!empty($blAnswers)) {
		$wrapper->add($blAnswers);
	}
}
$screen->add($wrapper);
$screen->initialize($data);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		
		try {
			
			if($question->exists()) {
				$question->setFields($fields);
				$question->prepareUpdate(Question::CHANGE_DEFAULT);
				$question->update();
				Fox\Message::Success(Msg(Messages::BUNDLE,
									Messages::SURVEY_QUESTION_HAS_BEEN_UPDATED));
			} else {
				$fields['surveyid'] = $keys['surveyid'];
				$fields['sectionid'] = $keys['sectionid'];
				$fields['language'] = $survey->language;
				$fields['mandatory_answers_nb'] = 0;//TODO change this
				
				if(!empty($keys['parentid'])) {
					$fields['parentid'] = $keys['parentid'];
				}
				$question->setFields($fields);
				$question->create();
				Fox\Message::Success(Msg(Messages::BUNDLE,
									Messages::SURVEY_QUESTION_HAS_BEEN_CREATED));
			}
			echo $xview->messages(true,true);
			exit;
		} catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
		echo $xview->messages(true,false);
		exit;
	}
}
$popup = new Popup('pQuestion',$label,$screen,600);
echo $popup->display();